#include "sessionFriendArea.h"


///////////////////////////
/// 会话区域
//////////////////////////
SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}
{
    setWidgetResizable(true);



    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width: 2px; background-color: rgb(240,240,240);}");
    horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height: 0px; }");
    setStyleSheet("QWidget{border: none;}");
    container = new QWidget;
    container->setFixedWidth(250);
    this->setWidget(container);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->setSpacing(0);
    vLayout->setAlignment(Qt::AlignTop);
    container->setLayout(vLayout);


#ifdef TEST_UI
    QIcon icon(":/resource/Image/avatar_1.png");
    for(int i = 0; i < 20; i++)
    {
        addItem(ApplyItemType, QString::number(i), icon, QString("测试人物")+QString::number(i+1), QString("这是一段中二的签名~~"));
    }

#endif
}

void SessionFriendArea::addItem(ItemType itemType, const QString& itemId, const QIcon& avatar, const QString& name, const QString& text)
{
    SessionFriendItem* item = nullptr;
    if(SessionItemType == itemType)
    {
        item = new SessionItem(this, itemId, avatar, name, text);
    }else if(FriendItemType == itemType)
    {
        item = new FriendItem(container, itemId, avatar, name, text);
    }else if(ApplyItemType == itemType)
    {
        item = new ApplyItem(container, itemId, avatar, name);
    }
    else
    {
        LOG() << "item类型错误" ;
        return ;
    }

    container->layout()->addWidget(item);
}

void SessionFriendArea::clickItem(int index)
{
    if(index < 0 || index >= container->layout()->count())
    {
        LOG() << "clickItem() -> index越界 index: " << index;
        return ;
    }

    SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(container->layout()->itemAt(index));
    item->select();
}

////////////////////////
/// 会话区域中的Item块
////////////////////////////


SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text)
{
    setFixedHeight(70);
    setStyleSheet("QWidget { background-color: rgb(247, 247, 247); }");

    QGridLayout* gLayout = new QGridLayout;
    gLayout->setSpacing(15);

    setLayout(gLayout);

    QPushButton *avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50,50);
    avatarBtn->setIconSize(QSize(50,50));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton{border: none;}");
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    QLabel *nameLabel = new QLabel;
    nameLabel->setText(name);
    nameLabel->setStyleSheet("QLabel{font-size: 18px; font-weight: 600;}");
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    messageLabel = new QLabel;
    messageLabel->setText(text);
    messageLabel->setFixedHeight(35);
    messageLabel->setStyleSheet("QLabel{font-size: 14px; font-weight: 400;}");
    messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);


    gLayout->addWidget(avatarBtn, 0,0,2,2);
    gLayout->addWidget(nameLabel, 0, 2, 1,8);
    gLayout->addWidget(messageLabel, 1, 2, 1, 8);


}

void SessionFriendItem::select()
{
    QObjectList obj = parentWidget()->children();

    for(QObject* e : obj)
    {
        if(!e->isWidgetType()) continue;
        SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(e);
        if(item->selected)
        {
            item->setStyleSheet("QWidget { background-color: rgb(247, 247, 247); }");
            item->selected = false;
        }
    }

    setStyleSheet("QWidget { background-color: rgb(210, 210, 210); }");
    selected = true;
}

void SessionFriendItem::mousePressEvent(QMouseEvent *event)
{

    select();

}

void SessionFriendItem::enterEvent(QEnterEvent *event)
{
    (void) event;

    // 当前这个 item 是选中状态, 则背景色不受到该逻辑影响
    if (this->selected) {
        return;
    }
    // 设置一个更深的颜色
    this->setStyleSheet("QWidget { background-color: rgb(210, 210, 210);}");
}

void SessionFriendItem::leaveEvent(QEvent *event)
{
    (void) event;

    // 当前这个 item 是选中状态, 则背景色不受到该逻辑影响
    if (this->selected) {
        return;
    }
    // 还原背景色
    this->setStyleSheet("QWidget { background-color: rgb(247, 247, 247);}");
}

void SessionFriendItem::paintEvent(QPaintEvent *event)
{
    (void) event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}


////////////////////////
/// 会话Item
////////////////////////////
///

SessionItem::SessionItem(QWidget* owner, const QString& sessionId, const QIcon& avatar, const QString& name, const QString& lastMessage)
    : SessionFriendItem(owner, avatar, name, lastMessage), _sessionId(sessionId), _lastMessage(lastMessage)
{

}

SessionItem::~SessionItem()
{

}
////////////////////////
/// 好友Item
////////////////////////////
///

FriendItem::FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name, const QString& description)
    : SessionFriendItem(owner, avatar, name, description), _userId(userId)
{

}

FriendItem::~FriendItem()
{

}
////////////////////////
/// 好友申请Item
////////////////////////////
///

ApplyItem::ApplyItem(QWidget* owner, const QString& applyId, const QIcon& avatar, const QString& name)
    : SessionFriendItem(owner, avatar, name, ""), _applyId(applyId)
{
    QGridLayout *gLayout = dynamic_cast<QGridLayout*>(this->layout());
    gLayout->removeWidget(messageLabel);
    delete messageLabel;

    QPushButton *acceptBtn = new QPushButton();
    acceptBtn->setText("同意");
    acceptBtn->setStyleSheet("QPushButton:hover{background-color:rgb(192, 192, 192);}");
    QPushButton *rejectBtn = new QPushButton();
    rejectBtn->setStyleSheet("QPushButton:hover{background-color:rgb(192, 192, 192);}");
    rejectBtn->setText("拒绝");

    gLayout->addWidget(acceptBtn, 1,2,1,1);
    gLayout->addWidget(rejectBtn, 1,3,1,1);


}

ApplyItem::~ApplyItem()
{

}




